import { GetShopListClassify } from '@/api/shop';
import shopService from '@/serivce/shopService';

const state = {
    ShopList: [],
    refreshShopTime: 1,
    refreshStockTime: 1,
    ShopClassList: []
}

const getters = {
    getRefreshShopTime(state) {
        return state.refreshShopTime
    },
    getRefreshStockTime(state) {
        return state.refreshStockTime
    }
}

const mutations = {
    SET_REFRESH_SHOP(state, val) {
        state.refreshShopTime = val
    },
    SET_REFRESH_STOCK(state, val) {
        state.refreshStockTime = val
    },
    GETSHOPCLASSLIST(state, data) {
        state.ShopClassList = data
        return data
    },
    UPDATESHOPCLASS(state, data) {
        return data
    }
}

const actions = {
    //商品列表
    async getShopList({ commit }, { currentPage, pageSize }) {
        try {
            let result = await shopService.getShopList(currentPage, pageSize);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //获取商品分类列表
    async getShopClassList({ commit }, currentPage, pageSize) {
        try {
            let result = await shopService.GetShopListClassify(currentPage, pageSize);
            commit('GETSHOPCLASSLIST', result.data.data)
            console.log(result.data);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //获取分类
    async getQueryClass() {
        try {
            let result = await shopService.getQueryClass();
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //搜索
    async searchShop({ commit }, [{ keyWords, categoryId, status, brand }, callback]) {
        try {
            let result = await shopService.searchShop(keyWords, categoryId, status, brand);
            callback(result.data)
        } catch (error) {
            console.log(error);
        }
    },
    //商品查看详情
    async checkShopDetail({ commit }, { id }) {
        try {
            let result = await shopService.checkShopDetail(id);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //删除
    async deleteShop({ commit }, id) {
        try {
            let result = await shopService.deleteShop(id);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //编辑
    async editShopInfo({ commit }, { prodId }) {
        try {
            let result = await shopService.editShopInfo(prodId);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //批量上架
    async batchUpShop({ commit }, ids ) {
        // console.log('13',ids);
        try {
            let result = await shopService.batchUpShop(ids);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //批量下架
    async batchDownShop({ commit }, ids ) {
        try {
            let result = await shopService.batchDownShop(ids);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //标签
    async shopListTag({ commit }, data ) {
        try {
            let result = await shopService.shopListTag(data);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    /***************************************************************************/

    //商品审核
    async getShopAuditList({ commit }, { currentPage, pageSize }) {
        try {
            let result = await shopService.getShopAuditList(currentPage, pageSize);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //搜索
    async searchShopAudit({ commit }, [{ keyWords, categoryId, brand, status }, callback]) {
        try {
            let result = await shopService.searchShopAudit(keyWords, categoryId, brand, status);
            callback(result.data)
        } catch (error) {
            console.log(error);
        }
    },
    //删除
    async deleteAudit({ commit }, id) {
        try {
            let result = await shopService.deleteAudit(id);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //审核
    async auditShop({ commit }, data) {
        try {
            let result = await shopService.auditShop(data);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //标签
    async auditListTag({ commit }, data ) {
        try {
            let result = await shopService.auditListTag(data);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    /***************************************************************************/

    //商品评价
    async getAssessList({ commit }, { currentPage, pageSize }) {
        try {
            let result = await shopService.getAssessList(currentPage, pageSize);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //查看评价详情
    async checkAssessDetail({ commit }, { id }) {
        try {
            let result = await shopService.checkAssessDetail(id);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //搜索
    async queryAssess({ commit }, [{ prodName, nickName }, callback]) {
        try {
            let result = await shopService.queryAssess(prodName, nickName);
            callback(result.data)
        } catch (error) {
            console.log(error);
        }
    },
    //删除
    async deleteAssess({ commit }, idList) {
        // console.log('7',idList);
        try {
            let result = await shopService.deleteAssess(idList);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //回复
    async replyAssess({ commit }, { replyContent }) {
        try {
            let result = await shopService.replyAssess(replyContent);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //是否显示评论
    async showAssess({ commit }, { prodCommId,status }) {
        console.log('18',prodCommId,status );
        try {
            let result = await shopService.showAssess(prodCommId,status);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    /***************************************************************************/

    //商品库存
    async getShopStockList({ commit }, { currentPage, pageSize }) {
        try {
            let result = await shopService.getShopStockList(currentPage, pageSize);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //删除库存商品
    async delShopStock({ commit }, { skuId }) {
        try {
            let result = await shopService.delShopStock(skuId);
            return result.data.data;
        } catch (error) {
            console.log(error);
        }
    },
    //批量删除
    async batchDelShopStock({ commit }, ids) {
        try {
            console.log('7', ids);
            let result = await shopService.batchDelShopStock(ids);
            return result.data.data;
        } catch (error) {
            console.log(error);
        }
    },
    //单个是否启用
    async singleUpDownOne({ commit }, {skuId,status}) {
        try {
            let result = await shopService.singleUpDownOne(skuId,status);
            return result.data.data;
        } catch (error) {
            console.log(error);
        }
    },

    /***************************************************************************/

    //商品回收站
    async inquireRecycle({ commit }, { currentPage, pageSize }) {
        try {
            let result = await shopService.inquireRecycle(currentPage, pageSize);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    // 获取二级商品分类列表
    async getShopClassListSecond({ commit }, id, currentPage, pageSize) {
        try {
            let result = await shopService.GetShopListSecond(id, currentPage, pageSize);
            console.log(result.data);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //搜索
    async searchShopRecycle({ commit }, [{ prodName, categoryId }, callback]) {
        try {
            let result = await shopService.searchShopRecycle(prodName, categoryId);
            callback(result.data)
        } catch (error) {
            console.log(error);
        }
    },
    //删除回收站商品
    async deleteRecycleShop({ commit }, { prodId }) {
        try {
            console.log('1', prodId);
            let result = await shopService.deleteRecycleShop(prodId);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    // 获取所有分类列表
    async GetAllClass() {
        try {
            let result = await shopService.GetAllClass();
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //批量删除
    async batchDeleteRecycleShop({ commit }, idList ) {
        try {
            // console.log('1', prodId);
            let result = await shopService.batchDeleteRecycleShop(idList);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    //更新数据(商品分类开关)
    async UpdateShopClass({ commit }, data) {
        try {
            let result = await shopService.UpdateShopClass(data);
            console.log('更新数据    ', result);
            commit('UPDATESHOPCLASS', result.data.data)
            return result.data
        } catch (error) {
            console.log(error);
        }
    },
    //还原
    async reductionRecycleShop({ commit }, prodId) {
        try {
            let result = await shopService.reductionRecycleShop(prodId);
            return result.data.data
        } catch (error) {
            console.log(error);
        }
    },
    // 根据商品id查询商品详情
    async GetShopListById({ commit }, id) {
        try {
            let result = await shopService.GetShopListById(id);
            return result.data
        } catch (error) {
            console.log(error);
        }
    },

    //自动请求商品信息
    autoGetShop({ commit }, hour) {
        commit('SET_REFRESH_SHOP', hour)
    },
    //自动请求库存数据
    autoGetStock({ commit }, hour) {
        commit('SET_REFRESH_STOCK', hour)
    },

    // 修改数据
    async EditShopClass({ commit }, data) {
        try {
            console.log('store', data);
            let result = await shopService.EditShopClass(data);
            return result
        } catch (error) {
            console.log(error);
        }
    },

    // 添加一级分类和所有分类
    async AddShopClass({ commit }, data) {
        // console.log( categoryName, garde,seq, status,icon, description);
        try {
            let result = await shopService.AddShopClass(data);
            return result
        } catch (error) {
            console.log(error);
        }
    },

    // 根据分类id删除分类
    async DelShopClassById({ commit }, id) {
        try {
            let result = await shopService.DelShopClassById(id);
            return result
        } catch (error) {
            console.log(error);
        }
    },

    // 单文件上传
    async UploadFile({ commit }, data) {
        try {
            let result = await shopService.UploadFile(data);
            return result
        } catch (error) {
            console.log(error);
        }
    },

    // 多文件上传
    async UploadFiles({ commit }, data) {
        try {
            let result = await shopService.UploadFiles(data)
            return result
        } catch (error) {
            console.log(error);
        }
    },

    // 新增商品
    async AddShopInfo({commit},data){
        try{
            let result = await shopService.AddShopInfo(data)
            return result
        }catch(error){
            console.log(error);
        }
    }


};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}

// export default {
//     namespaced: true,
//     state: {
//         ShopList: []
//     },
//     getters: {
//         GetShopList(state) {
//             return [...state.ShopList]
//         }
//     },
//     actions: {//异步操作写在actions中
//         getShopListInfo({ commit }, { currentPage, pageSize }) {
//             shopService.getShopList(currentPage, pageSize).then(res => {
//                 console.log('1',res.data);
//                 commit('GETSHOPLIST', res.data.data);
//             });
//         }
//     },
//     mutations: {
//         GETSHOPLIST(state, data) {
//             state.ShopList = data
//         }
//     }
// }
